package CutLoad.cookies
{
	import CutLoad.Base;
	
	import flash.events.Event;
	/**
	 * 每一个时间结束，调用一次这个方法。 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class QueueTimeRun extends Base
	{
		private var arrFrameFps:Array;
		private var time_i:Number=0;
		private var time_i_count:Number=0;
		private var index:Number=0;
		private var funDo:Function;
		private var isPlay:Boolean;
		/**
		 * 生成若干相同数量的时间 
		 * @param frameCount 以帧数为单的时间
		 * @param numCount 几个
		 * @param funDo  执行的方法:function(第几个时间下标从0开始:Number)
		 * @return 
		 * 
		 */		
		public static function getSomeTime(frameCount:Number,numCount:Number,funDo:Function):QueueTimeRun{
			var arr:Array=new Array();
			for(var i:Number=0;i<numCount;i++){
				arr.push(frameCount);
			}
			return new QueueTimeRun(arr,funDo);
		}
		/**
		 * 初始化 
		 * @param arrFrameFps 以帧数为单的时间数组
		 * @param funDo 执行的方法:function(第几个时间下标从0开始:Number)
		 * 
		 */		
		public function QueueTimeRun(arrFrameFps:Array,funDo:Function)
		{
			isPlay=false;
			this.funDo=funDo;
			time_i=0;
			this.arrFrameFps=arrFrameFps;
			time_i_count=arrFrameFps[index]
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
		}
		/**
		 * 开始运行 
		 * 
		 */		
		public function start():void{
			isPlay=true;
		}
		/**
		 * 暂时停下来 
		 * 
		 */		
		public function stop():void{
			isPlay=false;
		}
		private function enterFrame(e:Event):void{
			if(!isPlay) return;
			if(time_i++>time_i_count){
				try{
					funDo(index);
				}catch(e:ArgumentError){
					funDo();
				}
				
				index++;
				if(index>arrFrameFps.length-1){
					index=arrFrameFps.length-1;
					this.removeEventListener(Event.ENTER_FRAME,enterFrame);
					this.killMe();
					isPlay=false;
					return;
				}
				time_i_count=arrFrameFps[index];
				time_i=0;
			}
		}
	}
}